<%--
  Created by IntelliJ IDEA.
  User: yangbushudemao
  Date: 2021/5/4
  Time: 9:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="/common/common.jsp" %>
<html>
<head>
    <title>zhanshi</title>
</head>
<body>
<form id="Viewthecar">
    汽车名称:<INPUT type="text" name="cname">
    汽车类型:<select name="tid">
                <option value="-1">请选择</option>
                <option name="tid" value="1">轿车</option>
                <option name="tid" value="2">客车</option>
                <option name="tid" value="3">越野车</option>
                <option name="tid" value="4">牵引车</option>
             </select>
    <input type="button" value="sousuo" onclick="getCar()">
    <div id="Exhibition">

    </div>
    <jsp:include page="/common/page.jsp"/>
</form>
</body>
</html>
<script>
    $(function () {
        getCar();
    })

    function getCar() {
        $.ajax({
            url:"<%=path%>/CarController/getViewthecar",
            type:"post",
            data:$("#Viewthecar").serialize(),
            datatype:"json",
            asyuc:false,
            success:function (parameter) {
                let list = parameter.list;
                let table = "<table border='1px' cellspacing='0' width='100%'>";
                table += "<tr>";
                table += "<td><input type='button' value='删除选中汽车' onclick='deleteChecks()'></td>";
                table += "<td colspan='4'>汽车展示</td>";
                table += "<td><input type='button' value='增加新汽车' onclick='toSave()'></td>";
                table += "</tr>";
                table += "<tr>";
                table += "<tr>";
                table += "<th>" +
                    "<input type='button' value='反选' onclick='reverseBox()'>" +
                    "<input type='checkbox'  onclick='selectAll(this)'>全选</th>";
                table += "<th>cname</th>";
                table += "<th>cprice</th>";
                table += "<th>cdate</th>";
                table += "<th>tid</th>";
                table += "<th>操作</th>";
                table += "</tr>";
                for (let i = 0;i<list.length;i++){
                    table +="<tr>";
                    table +="<td><input type='checkbox' name='boxs' value='" +list[i].cid + "'></td>"
                    table += "<td>" + list[i].cname+ "</td>";
                    table += "<td>" + list[i].cprice + "</td>";
                    table += "<td>" +list[i].cdate+ "</td>";
                    table += "<td>"
                    if(list[i].tid == 1){
                        table += "轿车";
                    }else if(list[i].tid == 2){
                        table += "客车";
                    }else if(list[i].tid == 3){
                        table += "越野车";
                    }else if(list[i].tid == 4){
                        table += "牵引车";
                    }
                    table += "</td>";
                    table += "<td>"
                    table += "<input type='button' value='修改' onclick='toUpdate(" + list[i].cid+ ")'>"
                    table += "<input type='button' value='删除' onclick='Deletecar(" + list[i].cid+ ")'>";
                    table += "</td>";
                    table +="</tr>";
                }
                table += "</table>"
                //根据div的id填充进去
                $("#Exhibition").html(table);
                pageInstall(parameter);
            },
            error:function () {
                alert("代码错了")
            }
        })
    }
    //fanxuan
    function reverseBox() {
        $('[name=boxs]').each(function (a,b) {
            !$(b).prop("checked",!$(b).prop("checked"))
        })
    }
    //quanxuan
    function selectAll(obj) {
        $("[name=boxs]").each(function (index,e) {
            $(e).prop("checked",$(obj).prop("checked"))
        })
    }
    //zengjia
    function toSave() {
        location.href="<%=path%>/CarController/toadd"
    }
    //piliangshanchu
    function deleteChecks() {
        let arr = [];
        let $chds =$("[name=boxs]:checked");
        $($chds).each(function (a,b) {
            arr.push(b.value)
        })
        if("宝,确定要删我吗"){
            $.ajax({
                url:"<%=path%>/CarController/deleteChecks",
                type:"post",
                data:{arr:arr},
                datatype:"json",
                async:false,
                success:function (canshu) {
                    if(canshu == 1){
                        alert("shanchu 成功返回到暂时页面");
                        location.href="<%=path%>/CarController/list";
                    }
                },
                error:function () {
                    alert("shanchu失败")
                }
            })
        }
    }

    function toUpdate(cid) {
        location.href="<%=path%>/CarController/toupdate?cid="+cid;
    }
    function Deletecar(cid) {
        location.href="<%=path%>/CarController/delete?cid="+cid;
    }
</script>
